package com.levy.contronller.lamdaTest.stream;

import antlr.collections.impl.LList;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class StreamTest3 {

    @Test
    public void test1() {

        List<String> list = Arrays.asList("hello", "你好");
        List<String> list1 = Arrays.asList("zhangsna", "lisi", "zhaoliu");
        String sex = s1.getSex();

        list.stream().flatMap(item -> list1.stream().map(item1 -> item + " " + item1))
                .collect(Collectors.toList()).forEach(System.out::println);
    }

    Student s1 = new Student("jack1", "m", 80);
    Student s2 = new Student("jack2", "f", 90);
    Student s3 = new Student("jack3", "m", 60);
    Student s4 = new Student("jack2", "f", 10);
    Student s5 = new Student("jack1", "f", 30);

    /**
     * 分组
     */
    @Test
    public void test2() {

        List<Student> list = Arrays.asList(s1, s2, s3, s4, s5);

        Map<String, List<Student>> collect = list.stream().collect(Collectors.groupingBy(s -> s.getSex()));
        Map<String, List<Student>> collect2 = list.stream().collect(Collectors.groupingBy(Student::getSex));
        System.out.println(collect);

        Map<String, Long> c = list.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.counting()));
        System.out.println(c);


        Map<String, Double> a = list.stream().collect(Collectors.groupingBy(Student::getName, Collectors.averagingDouble(Student::getScore)));
        System.out.println(a);

        System.out.println(list.stream().collect(Collectors.counting()));
        System.out.println(list.stream().count());

    }


    /**
     * 分区
     */
    @Test
    public void test3() {

        List<Student> list = Arrays.asList(s1, s2, s3, s4, s5);

        Map<Boolean, List<Student>> collect = list.stream().collect(Collectors.partitioningBy(s -> s.getScore() > 60));
        System.out.println(collect);

    }

    @Test
    public void test4() {
        List<String> list = Arrays.asList("hell", "wfsdfodfsr", "dddfdsd");
        list.stream().mapToInt(item -> item.length())
                .filter(len -> len == 4).findFirst()
                .ifPresent(System.out::println);

//        这个不会执行 是由于没有
        list.stream().mapToInt(item -> {
            int length = item.length();
            System.out.println(length);
            return length;
        });


    }

}


class Student {
    private String name;

    private String sex;

    private int score;

    public Student(String name, String sex, int score) {
        this.name = name;
        this.sex = sex;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"name\":\"")
                .append(name).append('\"');
        sb.append(",\"sex\":\"")
                .append(sex).append('\"');
        sb.append(",\"score\":")
                .append(score);
        sb.append('}');
        return sb.toString();
    }
}
